/*
 * Copyright 2008 Android4ME
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package test;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;

import zip.tool.Decompression;
import android.content.res.AXmlResourceParser;
import android.util.TypedValue;

/**
 * @author Dmitry Skiba
 * 
 * This is example usage of AXMLParser class.
 * 
 * Prints xml document from Android's binary xml file.
 */
public class ApkInfoDemo {
	public static class ApkInfo {
		public String versionCode;
		public String versionName;
		public String packageName;
		
		@Override
		public String toString(){
			return "[versionCode:"+versionCode+",versionName:"+versionName+",packageName:"+packageName+"]";
		}
	}

	public static void main(String[] arguments) {
		String path = "D:\\s3_uploader\\YY01\\01.apk";
		
		Decompression d = new Decompression(path);
		ApkInfo apkInfo = parse(d.getAndroidManifest());
		System.out.println(apkInfo.toString());
	}
	
	public static ApkInfo parse(InputStream stream){		
		ApkInfo result = new ApkInfo();
		if (stream == null) {
			return result;
		}
		try {
			AXmlResourceParser parser=new AXmlResourceParser();
			parser.open(stream);
			while (true) {
				int type=parser.next();
				if (type==XmlPullParser.END_DOCUMENT) {
					break;
				}
				switch (type) {
					case XmlPullParser.START_TAG:
					{
						if ("manifest".equals(parser.getName())){
							result.versionCode = parser.getAttributeValue("http://schemas.android.com/apk/res/android", "versionCode");
							result.versionName = parser.getAttributeValue("http://schemas.android.com/apk/res/android", "versionName");
							result.packageName = parser.getAttributeValue(null, "package");
						}
						
						return result;
					}
					
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
}